﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace rfiInbox
{
    public class dayscalc
    {
        public static DateTime today = DateTime.Today;
        public static int businessdays;
        public static HashSet<DateTime> holidays = new HashSet<DateTime>();
        
        
        public static int numberofdays(DateTime date, ref int days)
        {
            days = today.Day - date.Day;
            if (days == 0)
            {
                return days;
            }
            else
            {
                for (DateTime i = date; i <= today; i = i.AddDays(1))
                {
                    if (i.DayOfWeek == DayOfWeek.Sunday)
                    {
                        days = days - 1;
                        //date = date.AddDays(1);
                    }
                    else if (i.DayOfWeek == DayOfWeek.Saturday)
                    {
                        days = days - 1;
                        //date = date.AddDays(1);
                    }
                    else if (holidays.Contains(i))
                    {
                        days = days - 1;
                        //date = date.AddDays(1);
                    }
                    //date = date.AddDays(1);
                }
                return days;
            }
            
        }

        public static HashSet<DateTime> GetHolidays(int year)
        {
            //holidays = new HashSet<DateTime>();
            //NEW YEARS 
            DateTime newYearsDate = AdjustForWeekendHoliday(new DateTime(year, 1, 1).Date);
            holidays.Add(newYearsDate);
            //MEMORIAL DAY  -- last monday in May 
            DateTime memorialDay = new DateTime(year, 5, 31);
            DayOfWeek dayOfWeek = memorialDay.DayOfWeek;
            while (dayOfWeek != DayOfWeek.Monday)
            {
                memorialDay = memorialDay.AddDays(-1);
                dayOfWeek = memorialDay.DayOfWeek;
            }
            holidays.Add(memorialDay.Date);

            //INDEPENDENCE DAY 
            DateTime independenceDay = AdjustForWeekendHoliday(new DateTime(year, 7, 4).Date);
            holidays.Add(independenceDay);

            //LABOR DAY -- 1st Monday in September 
            DateTime laborDay = new DateTime(year, 9, 1);
            dayOfWeek = laborDay.DayOfWeek;
            while (dayOfWeek != DayOfWeek.Monday)
            {
                laborDay = laborDay.AddDays(1);
                dayOfWeek = laborDay.DayOfWeek;
            }
            holidays.Add(laborDay.Date);

            //THANKSGIVING DAY - 4th Thursday in November 
            var thanksgiving = (from day in Enumerable.Range(1, 30)
                                where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday
                                select day).ElementAt(3);
            DateTime thanksgivingDay = new DateTime(year, 11, thanksgiving);
            holidays.Add(thanksgivingDay.Date);

            //CHRISTMAS DAY
            DateTime christmasDay = AdjustForWeekendHoliday(new DateTime(year, 12, 25).Date);
            holidays.Add(christmasDay);
            return holidays;
        }

        public static DateTime AdjustForWeekendHoliday(DateTime holiday)
        {
            if (holiday.DayOfWeek == DayOfWeek.Saturday)
            {
                return holiday.AddDays(-1);
            }
            else if (holiday.DayOfWeek == DayOfWeek.Sunday)
            {
                return holiday.AddDays(1);
            }
            else
            {
                return holiday;
            }
        }
    
internal static void datecalculator(DateTime recd)
{
 	throw new NotImplementedException();
}}
}
